<template>
	<view>
		<u-input v-model="value" :type="type" :border="border" :auto-height="false" :height="500" :clearable="false"
			border='true' maxlength="999999999999" placeholder="请复制dpf的Base64编码" />
		<view class="ubutton">
			<u-button @click="Base64ToDpf">Base64转换dpf</u-button>
		</view>

	</view>
</template>

<script>
	export default {
		name: "base64ToDpf",
		data() {
			return {
				value: '',
				inputStyle: {
					'text-overflow': 'unset',
					'white-space': 'unset',
					'overflow': 'unset'
				},
				type: 'textarea'
			};
		},
		methods: {
			Base64ToDpf() {
				let result = this.value.replace(/[\r\n]/g, '');
				let pdfBase64 = `data:application/pdf;base64,${result}`;
				var fs = wx.getFileSystemManager();
				let fileName = '';

				const timestamp = new Date().getTime(); // 获取当前时间戳
				const date = new Date(timestamp); // 创建Date对象
				const year = date.getFullYear(); // 获取年份
				const month = date.getMonth() + 1; // 获取月份，需要加1，因为月份从0开始
				const day = date.getDate(); // 获取日期
				const hours = date.getHours(); // 获取小时
				const minutes = date.getMinutes(); // 获取分钟
				const seconds = date.getSeconds(); // 获取秒数
				// 将结果格式化为字符串
				const formattedDate = `${year}-${month}-${day}`;

				fs.writeFile({ //保存本地临时路径
					filePath: wx.env.USER_DATA_PATH + '/' + formattedDate + '.pdf',
					data: wx.base64ToArrayBuffer(result.replace(/[\r\n]/g, '')),
					success: res => {
						let dpf = wx.env.USER_DATA_PATH + '/' + formattedDate + '.pdf';
						wx.openDocument({
							fileType: 'pdf',
							showMenu: true,
							filePath: wx.env.USER_DATA_PATH + '/' + formattedDate + '.pdf',
							success: function(res) {

							},
							fail(err) {

							}
						});
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	.ubutton {
		margin: 20upx 0;
	}
</style>